package com.mhkj.execute.feedback.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.earlyWarning.entity.EarlyWarning;
import com.mhkj.base.earlyWarning.service.EarlyWarningService;
import com.mhkj.execute.feedback.entity.PlanFeedBack;
import com.mhkj.execute.feedback.service.PlanFeedBackService;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mhkj.revise.professional.service.ProfessionalPlanNodeService;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.util.DateUtils;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.base.entity.FileEntity;
import com.mics.core.system.base.service.FileService;

/**
 * 计划反馈操作接口
 * 
 * @author ypx
 * @date 2018年7月2日
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/planFeedBack")
public class PlanFeedBackController extends BaseController<PlanFeedBack> {

	@Autowired
	private FileService fileService;
	// @Autowired
	// private DeptMonPlanService deptMonPlanService;
	// @Autowired
	// private PerMonPlanService perMonPlanService;
	@Autowired
	private PlanFeedBackService planFeedBackService;
	@Autowired
	private PlanNodeService planNodeService;

	@Resource
	private EarlyWarningService earlyWarningService;
	@Resource
	private ProfessionalPlanNodeService professionalPlanNodeService;

	@Resource
	private JsonHandler jsonHandler;
	/**
	 * 计划反馈
	 * 
	 * @author ypx
	 * @date 2018年7月2日
	 * @version 1.0
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(PlanFeedBack entity) {

		PlanNode planNode = planNodeService.getById(entity.getPlanNode().getId());
		entity.setPlanNode(planNode);
		String message = planFeedBackService.save(entity);
		if("ok".equals(message)) {
			
			
			if(planNode.getRate()<entity.getCurrentProgress()) {
				planNode.setRate(entity.getCurrentProgress());
				int state=planNode.getState();
				if(planNode.getRate()==100) {//当进度填报为100%时
					if(state==7) {//如果逾期 修改状态为6 逾期完成
						planNode.setState(6);
					}else {//其他 已完成
						planNode.setState(5);
					}
				}else {
					if(state==1) {//如果为未开始1 修改进行中状态 
						planNode.setState(2);
					}
				}
				planNodeService.edit(planNode);
			}
			


			Date now = DateUtils.getDateYMD(new Date(), "yyyy-MM-dd");
			List<EarlyWarning> earlyWarnings = earlyWarningService.findAll(); 
			for(EarlyWarning earlyWarning : earlyWarnings){
				int useDay = DateUtils.compareDay(now, planNode.getBeginDate());
				int deadlineRate = useDay / planNode.getDeadline()*100;
				if(deadlineRate >= earlyWarning.getWorkDayLeast() && deadlineRate < earlyWarning.getWorkDayMax() && planNode.getRate() < earlyWarning.getSchedule()){
					List<PlanNode> planNodeTemps = professionalPlanNodeService.getSameNameNode(planNode.getId());
					for(PlanNode planNodeTemp:planNodeTemps){
						planNodeTemp.setRiskState(1);
						professionalPlanNodeService.edit(planNodeTemp);
					}
				}else {
					List<PlanNode> planNodeTemps = professionalPlanNodeService.getSameNameNode(planNode.getId());
					for(PlanNode planNodeTemp:planNodeTemps){
						planNodeTemp.setRiskState(0);
						professionalPlanNodeService.edit(planNodeTemp);
					}
					
				}
			}
		}
		// 获取变更后的施工方案名
		String fileName = entity.getFileName();
		if (fileName != null && !fileName.isEmpty()) {
			String[] fileNameArr = fileName.split(";");
			// 获取变更后的施工方案名URL
			String fileUrl = entity.getFileUrl();
			String[] fileUrlArr = fileUrl.split(";");

			// 循环插入数据库
			for (int i = 0; i < fileNameArr.length; i++) {
				FileEntity fileEntity = new FileEntity();
				fileEntity.setTableId(entity.getId());
				fileEntity.setTableName("t_plan_feed_back");
				fileEntity.setFileName(fileNameArr[i]);
				fileEntity.setFileUrl(fileUrlArr[i]);
				fileService.save(fileEntity);
			}
		}
	/*	if(entity.getCurrentProgress()==100) {
			KeyFruitArchives archives=new KeyFruitArchives();
			FruitType fruitType=entity.getFruitType();
			PlanNode planNode=entity.getPlanNode();
			archives.setFruitType(fruitType);
			archives.setKeyType(planNode.getNodeProperty());
			archives.setPlanNode(planNode);
			keyFruitArchivesService.save(archives);
		}*/
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 修改 计划反馈
	 * 
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResponseMessage edit(@RequestBody PlanFeedBack entity) {
		String message = planFeedBackService.edit(entity);
		// 获取变更后的施工方案名
		String fileName = entity.getFileName();
		if (fileName != null && !fileName.isEmpty()) {
			String[] fileNameArr = fileName.split(";");
			// 获取变更后的施工方案名URL
			String fileUrl = entity.getFileUrl();
			String[] fileUrlArr = fileUrl.split(";");

			// 循环插入数据库
			for (int i = 0; i < fileNameArr.length; i++) {
				FileEntity fileEntity = new FileEntity();
				fileEntity.setTableId(entity.getId());
				fileEntity.setTableName("t_plan_feed_back");
				fileEntity.setFileName(fileNameArr[i]);
				fileEntity.setFileUrl(fileUrlArr[i]);
				fileService.save(fileEntity);
			}
		}
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 删除计划反馈
	 * 
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage del(Long id) {
		PlanFeedBack planFeedBack = new PlanFeedBack();
		planFeedBack.setId(id);
		String message = planFeedBackService.del(planFeedBack);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 查询反馈信息
	 * 
	 * @author ypx
	 * @date 2018年7月2日
	 * @version 1.0
	 */
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@ResponseBody
	public void info(HttpServletResponse response,Long id) {
		PlanFeedBack planFeedBack = planFeedBackService.getById(id);
		jsonHandler.sendJsonString(response, true, "获取数据成功", planFeedBack,
				new String[] { "id","planNode.plan.name","planNode.nodeName","completeIns","planNode.plan.planType.value","planNode.beginDate","planNode.endDate","expectDate","currentProgress" ,"isComplete","overDate"},
				new String[] { "id","planName","planNodeName","remark","planTypeName" ,"beginDate","endDate","expectDate","currentProgress","isComplete","overDate"});

	}

	/**
	 * 获取计划反馈分页列表
	 */
	@RequestMapping(value = "/listByPage", method = RequestMethod.POST)
	@ResponseBody
	public void listByPage(HttpServletResponse response,PlanFeedBack planFeedBack, Integer pageIndex, Integer pageSize) {

		Pagination<PlanFeedBack> findPage = planFeedBackService.findPage(planFeedBack, pageIndex, pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功！", findPage, new String[]{"id","status","currentProgress","createDate","createName","completeIns"},
				"id","status","currentProgress","createDate","createName","completeIns");

	}

	/**
	 * 获取计划反馈分页列表
	 */
	@RequestMapping(value = "/changeByStatus", method = RequestMethod.POST)
	@ResponseBody
	// 定时任务
	@Scheduled(cron = "0 0 8 ? * * ")
	public String changeByStatus() {
		String changeByStauts = planFeedBackService.changeByStauts();
		System.out.println(changeByStauts);
		return changeByStauts;

	}

}
